<template>
    <el-form ref="ruleForm" :rules="currentRules" :model="form.data" label-width="120px">
        <el-row>
            <form-item v-for="(column, prop) in form.columns" :key="prop" :visible="column.visible" :prop="prop"
                v-bind="column" v-model="form.data[prop]" :formspan="24">
            </form-item>
        </el-row>

        <el-row>
            <el-col :span="24" class="dialog-footer">
                <el-button @click="dialogCancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </el-col>
        </el-row>
    </el-form>
</template>
  
<script>
import formitem from "@/components/form/item";
export default {
    components: {
        "form-item": formitem,
    },
    data() {
        return {
            form: {
                columns: {
                    ParentId: {
                        componentName: "form-Cascader",
                        label: "父级菜单",
                        rules: "required",
                        options: [],
                        clearable: true,
                        props: {
                            checkStrictly: true,
                            value: "MenuId",
                            label: "MenuText",
                        },
                    },
                    MenuCode: {
                        componentName: "form-input",
                        label: "菜单编码",
                        rules: "required",
                    },
                    MenuText: {
                        componentName: "form-input",
                        label: "菜单目录",
                        rules: "required",
                    },
                    Icon: {
                        componentName: "form-icon",
                        label: "菜单图标",
                        rules: "required",
                    },
                    Url: {
                        componentName: "form-input",
                        label: "菜单地址",
                    },
                    Sort: {
                        componentName: "form-inputNumber",
                        label: "排序",
                        rules: "required",
                    },
                    Desc: {
                        componentName: "form-input",
                        type: "textarea",
                        label: "备注",
                        maxlength: 2000,
                        autosize: {
                            minRows: 5,
                            maxRows: 10,
                        },
                    },
                    IsShow: {
                        componentName: "form-radioGroup",
                        label: "是否显示",
                        rules: "required",
                        span: 12,
                        options: [
                            {
                                label: "显示",
                                value: 1,
                            },
                            {
                                label: "隐藏",
                                value: 0,
                            },
                        ],
                    },
                    Enabled: {
                        componentName: "form-radioGroup",
                        label: "启停用",
                        rules: "required",
                        span: 12,
                        options: [
                            {
                                label: "启用",
                                value: 1,
                            },
                            {
                                label: "停用",
                                value: 0,
                            },
                        ],
                    },
                },
                data: {
                    Ordinal: 1,
                    IsShow: 1,
                    Enabled: 1,
                },
            },
        };
    },
    props: {
        pkid: {
            type: Number,
            default: 0,
        },
        getData: Array,
        anthCode:String
    },
    created() {
        this.currentRules = this.$core.formRules(this.form);
    },
    methods: {
        dialogCancel() {
            this.form.data = {};
            this.$emit("closeMoule", false);
        },
        GetEntity() {
            if (this.pkid > 0) {
                let query = {
                    user: "string",
                    target: "MenuInfo",
                    operation: "Get",
                    json: {
                        Condition: 'MenuId=@MenuId',
                        Params: {
                            MenuId: this.pkid
                        }
                    }
                }
                // 请求接口
                this.$http.generalAPI(query, (res) => {

                    if (res.Code == 200) {
                        //获取菜单信息
                        this.form.data = res.Data[0];
                         let parentId= this.form.data.ParentId;
                         this.form.data.ParentId=parentId==0?-1:parentId;
                    }
                });
            }
        },
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let form = this.form.data;
                    form.ParentId= form.ParentId === -1 ? 0 : form.ParentId
                    let query = {
                        user: "string",
                        target: "MenuInfo",
                        operation: this.pkid > 0 ? "Update" : "Create",
                        json: form
                    }
                    // 请求接口
                    this.$http.generalAPI(query,  (res) => {

                        if (res.Code == 200) {
                            //获取菜单信息
                            this.$msg.success("操作成功");
                            this.form.data = {};
                            this.$emit("closeMoule", true);
                        }
                    });
                }
            });
        },
    },
    watch: {
        pkid: {
            immediate: true,
            deep: true,
            handler(newVal, oldVal) {
                this.form.data = {};
                if (newVal > 0) {
                    this.GetEntity();
                }
            },
        },
        getData: {
            immediate: true,
            deep: true,
            handler(newVal, oldVal) {
                let firstData = [
                    {
                        MenuId:-1,
                        MenuText: "平台建设系统",
                        children: newVal
                    },
                ];
                this.form.columns.ParentId.options = firstData;
            },
        }
    },
};
</script>
  
<style scoped>
.dialog-footer button {
    float: right;
    height: 36px;
    line-height: 8px;
    margin-left: 5px;
}
</style>
  